﻿function generateStop(stopInfo, Routes, Trips) {
    try {
        var result = $("<span></span>");
        result.append(generateHeader(stopInfo));

        var infoPanel = $("<div class='StopInfoPanel'></div>");

        var FavouritesTrips = {};
        var RegularsTrips = {};
        for (var TripCode in Trips) {
            if (isFavourite(TripCode)) {
                FavouritesTrips[TripCode] = Trips[TripCode];
            }
            else {
                RegularsTrips[TripCode] = Trips[TripCode];
            }
        }

        //seperate trips into favourite and regular
        var favourites = generateTrips(FavouritesTrips, false);
        var regulars = generateTrips(RegularsTrips, true);

        //sort by arrival time
        if (favourites && favourites.length > 0) {
            var favouritesPanel = $("<div class='StopInfoPanelFavourites StopInfoSubPanel'></div>");
            for (var i = 0; i < favourites.length; i++)
            { favouritesPanel.append(favourites[i]); }
            infoPanel.append(favouritesPanel);
        }

        //sort by bus stop code+name
        if (regulars && regulars.length > 0) {
            var regularPanel = $("<div class='StopInfoPanelRegular StopInfoSubPanel'></div>");
            for (var i = 0; i < regulars.length; i++)
            { regularPanel.append(regulars[i]); }
            infoPanel.append(regularPanel);
        }

        result.append(infoPanel);
        return result;
    }
    catch (err) {
        alert(err);
    }
}

function favouriteIcon(favouriteCode) {
    var icon = $("<div class='Favourite'><img src='star.png'/></div>");
    if (isFavourite(favouriteCode))
    { icon.addClass("Yes"); }
    icon.click(function () {
        if (icon.hasClass("Yes")) {
            icon.removeClass("Yes");
            removeFavourite(favouriteCode);
        }
        else {
            icon.addClass("Yes");
            addFavourite(favouriteCode);
        }

        // Save and refresh
        savePref();
        refreshFromCache(true);
    })
    return icon;
}

function generateHeader(stopInfo) {
    var header = $("<div class='StopHeader'></div>");
    header.append(favouriteIcon(getStopCode(stopInfo)));
    var code = $("<span class='Code'></span>");
    code.text(stopInfo.Code);
    header.append(code);
    var name = $("<span class='Name'></span>");
    name.text(stopInfo.Description);
    header.append(name);
    return header;
}

function generateTrips(Trips, sortByCode) {
    var result = [];
    if (sortByCode) {
        var tempCode = [];
        var tempDOM = {};

        for (var tripIndex in Trips) {
            var sortableCode = makeSortable(tripIndex);
            tempDOM[sortableCode] = generateTrip(tripIndex, Trips[tripIndex]);
            tempCode.push(sortableCode);
        }
        tempCode.sort();
        for (var i = 0; i < tempCode.length; i++) {
            result.push(tempDOM[tempCode[i]]);
        }
    }
    else {
        for (var tripIndex in Trips) {
            result.push(generateTrip(tripIndex, Trips[tripIndex]));
        }
    }
    return result;
}

function makeSortable(tripCode) {
    var words = tripCode.split(' ');
    while (words[0].length < 4)
    { words[0] = "0" + words[0]; }

    return words.join(' ').toLowerCase();
}

function generateTrip(code, trip) {
    var now = new Date();

    var currentTrips = [];
    for (var i = 0; i < trip.length; i++) {
        var time = diffTimeMinutes(parseTime(trip[i]), now);
        if (time >= -1) {
            currentTrips.push(trip[i]);
        }
    }

    var result = $("<div class='RouteInfo'></div>");
    var name = $("<div class='RouteName'></div>");
    name.text(code);
    result.append(name);

    result.append(favouriteIcon(code));

    var trips = $("<ul class='Trips'></ul>");
    var maxToShow = Math.min(currentTrips.length, 4);
    var ft = [];
    for (var i = 0; i < maxToShow; i++) {
        var time = diffTimeMinutes(parseTime(currentTrips[i]), now);
        var item = $("<li></li>");
        item.text(time);
        trips.append(item);
    }

    result.append(trips);
    return result;
}

function diffTimeMinutes(timeA, timeB) {
    var diff = timeA.getTime() - timeB.getTime();
    return Math.round(diff / 60000);
}

function parseTime(time) {
    var parts = time.split(":");
    var today = new Date();
    return new Date(today.getFullYear(), today.getMonth(), today.getDate(), parts[0], parts[1], parts[2]);
}